package cn.piesat.scanning.business.xz.service.drtimpl;

import cn.hutool.core.util.StrUtil;
import cn.piesat.scanning.business.xz.service.BuildAlgorithmTaskService;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskPathParam;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskRuleParam;
import cn.piesat.scanning.dto.HtScanningMetadataDTO;
import cn.piesat.scanning.service.HtScanningMetadataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Ming
 * @version 1.0
 * @description: TODO
 * @date 2023/12/18 13:48
 */
@Service
public class BuildVSWIServiceImpl implements BuildAlgorithmTaskService {

    @Autowired
    private HtScanningMetadataService htScanningMetadataService;
    @Override
    public Map<String, Object> initAlgorithmParams(String adsType, String primaryFile, String filePath, AlgorithmTaskRuleParam taskRuleParam, AlgorithmTaskPathParam taskPathParam, Map<String, Integer> indexParams, String fileDate, String scanningPath,HtScanningMetadataDTO htScanningMetadataDTO) {
        Map<String, Object> algorithmParams = new HashMap<>();
        //BAS_NDVI_540000_L3_FY3D_MERSI_1000M_GLL_MOTD_20230701000000_20230710000000.TIFF
        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
        String[] split = fileName.split("_");
        //String productMode = split[8];
        String region = split[2];
        String level = split[3];
        String moduleIid = split[0];
        String startTime = split[9];
        HtScanningMetadataDTO metadataDTO = htScanningMetadataService.findMetadataByParam(startTime, level, taskPathParam.getParameter03(),
                taskPathParam.getParameter05(), region, taskPathParam.getParameter06(), moduleIid, taskPathParam.getParameter07());
        if (null == metadataDTO) {
            algorithmParams.put("parameter01", null);
        } else {
            Map<String, Object> params = new HashMap<>();
            params.put("primaryFile", primaryFile);
            params.put("auxPath", taskPathParam.getParameter02());
            params.put("satID", taskPathParam.getParameter03());
            params.put("L2File", taskPathParam.getParameter04() + metadataDTO.getFilePath());
            algorithmParams.put("parameter00", adsType);
            algorithmParams.put("parameter01", params);
        }
        return algorithmParams;
    }
}
